package com.subway.babynote.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.subway.babynote.syncdata.ClientDataRecord;
import com.subway.babynote.syncdata.ClientDataRecordBase;
import com.subway.babynote.syncdata.StringUtil;
import com.subway.babynote.syncdata.tableName;

public class query_foods_food extends cursorbase {
	SQLiteDatabase db = null;
	String[] wheres = new String[] {
			"WHERE foodname = ?",
			"WHERE food_id = ?",
			"WHERE food_prop_id = ?",
			"WHERE category = ?"
	};
	String tn = tableName.t_foods;
	String sql = null;

	public query_foods_food(SQLiteDatabase _db) {
		db = _db;
		sql = TableInfoList.instance.getsqls(tn).selectsql;
	}

	public ClientDataRecord.foodsRecord getFood(int food_id)
	{
		ClientDataRecord.foodsRecord fr = new ClientDataRecord.foodsRecord(0, 0, null, null, null, 0);
		fr.food_id = food_id;
		
		return (ClientDataRecord.foodsRecord )this.select(fr, 1,true);
	}
	public ClientDataRecord.foodsRecord getFood(String foodname)
	{
		ClientDataRecord.foodsRecord fr = new ClientDataRecord.foodsRecord(0, 0, null, null, null, 0);
		fr.foodname = foodname;
		
		return  (ClientDataRecord.foodsRecord )this.select(fr, 0,true);
	}
	public ClientDataRecord.list_ClientDataRecord getFoodbyprop(int food_prop_id)
	{
		ClientDataRecord.foodsRecord fr = new ClientDataRecord.foodsRecord(0, 0, null, null, null, 0);
		fr.food_prop_id = food_prop_id;
		
		return (ClientDataRecord.list_ClientDataRecord)this.select(fr, 2,false);
	}
	
	public ClientDataRecord.list_ClientDataRecord getFoodybyCategory(String category)
	{
		ClientDataRecord.foodsRecord fr = new ClientDataRecord.foodsRecord(0, 0, null, null, null, 0);
		fr.category = category;
		
		return (ClientDataRecord.list_ClientDataRecord)this.select(fr, 3,false);
	}
	
	private ClientDataRecordBase select(ClientDataRecordBase crb, int id, boolean select_1)
	{
		String[] strs = this.getSelectParam(crb, id);
		ClientDataRecordBase ret = null;
		Cursor cs  = null;
		try
		{
			cs = db.rawQuery(this.sql + this.wheres[id], strs);
			if(cs.getCount() > 0)
			{
				cs.moveToFirst();
				if(select_1)
				{
					ret = this.read(cs);
				}
				else
				{
					ret = new ClientDataRecord.list_ClientDataRecord();
					ClientDataRecord.list_ClientDataRecord ret2 = (ClientDataRecord.list_ClientDataRecord) ret;
					while(!cs.isAfterLast())
					{
						ClientDataRecordBase r = this.read(cs);
						if(r != null)
							ret2.list.add(r);
						cs.moveToNext();
					}
				}
			}
		}
		catch(Exception ex)
		{
			logging.err("querybase select", ex.getMessage());
			ret = null;
		}
		finally
		{
			this.releaseCursor(cs);
		}
		return ret;
	}
	
	
	protected String[] getSelectParam(ClientDataRecordBase crb, int id) {
		ClientDataRecord.foodsRecord fr = (ClientDataRecord.foodsRecord) crb;
		String str = null;
		switch(id)
		{
		case 0:
			str = fr.foodname;
			break;
		case 1:
			str = StringUtil.int2str(fr.food_id);
			break;
		case 2:
			str = StringUtil.int2str(fr.food_prop_id);
			break;
		case 3:
			str = fr.category;
			break;
		}
		// TODO Auto-generated method stub
		String[] strs = (str == null? null : new String[]{str} );
		return strs;
	}

	//food_id,food_prop_id,foodname,unit,category,ts
	protected ClientDataRecord.foodsRecord read(Cursor cs) {
		ClientDataRecord.foodsRecord fr = new ClientDataRecord.foodsRecord(0, 0, null, null, null, 0);
		fr.food_id = cs.getInt(0);
		fr.food_prop_id = cs.getInt(1);
		fr.foodname = cs.getString(2);
		fr.unit = cs.getString(3);
		fr.category = cs.getString(4);
		fr.ts = cs.getInt(5);
		return fr;
	}
}
